package model;

import java.sql.*;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class MaintananceModel {
	 Connection con = null;
     Statement stmt = null;
     ResultSet rs = null;
     
     MaintananceModel(){
    	 try {
	            Class.forName("oracle.jdbc.driver.OracleDriver");
	            con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521","gringotts","oksana");
	        
	        } catch (ClassNotFoundException e) {
	            e.printStackTrace();
	        } catch (SQLException e) {
	            e.printStackTrace();
	        }
     }
   ResultSet getAllInfoAboutBank() throws SQLException{
	   			stmt = con.createStatement();
	            rs = stmt.executeQuery("SELECT * FROM ATM");
		return rs;
	}
   
   void withdrawMoney(int amount) throws NoMoneyExeption, SQLException{
	   int []r =countToGiveMoney(amount);
	   int[] m=getMoneyUAN();
	   putMoneyUAN(m[0]-r[3],m[1]-r[2],m[2]-r[1],m[3]-r[0]);
   }
   
   int[] countToGiveMoney(int n) throws NoMoneyExeption, SQLException{
	   int[] arr=new int[4];
	   int[] masAtm =getMoneyUAN();
	   arr[0]=n/200;
	   while(arr[0]>masAtm[3])
		   --arr[0];
	   n=n-200*arr[0];
	   arr[1]=n/100;
	   while(arr[1]>masAtm[2])
		   --arr[1];
	   n=n-100*arr[1];
	   arr[2]=n/50;
	   while(arr[2]>masAtm[1])
		   --arr[2];
	   n=n-50*arr[2];
	   arr[3]=n/10;
	   while(arr[3]>masAtm[0])
		   --arr[3];
	   
	   if(n==0)
	   return arr;
	   else{
		   throw new NoMoneyExeption("no money");
	   }
   }
    void putMoneyUAN(int amount1,int amount2,int amount3,int amount4) throws SQLException{
        

    	 PreparedStatement ps = con.prepareStatement(
    		      "UPDATE ATM SET NUMDER_10 = ? , NUMDER_20= ? ,NUMDER_100= ? , NUMDER_200= ?  WHERE CURRENCY = ? ");

    		   
    		    ps.setInt(1,amount1);
    		    ps.setInt(2,amount2);
    		    ps.setInt(3,amount3);
    		    ps.setInt(4,amount4);
    		    ps.setString(5,"UAN");
    		    ps.executeUpdate();
    		    ps.close();
    }
	public void withdrawTen(int kilk) throws SQLException, NoMoneyExeption{
		int[] money =getMoneyUAN(); 
		if (money[0]>kilk){
			putMoneyUAN(money[0]-kilk,money[1],money[2],money[3]);
		}
		else{
			throw new  NoMoneyExeption("no money");
		}
			
	}
	public void withdrawFifty(int kilk) throws SQLException, NoMoneyExeption{
		int[] money =getMoneyUAN(); 
		if (money[1]>kilk){
			putMoneyUAN(money[0],money[1]-kilk,money[2],money[3]);
		}
		else{
			throw new  NoMoneyExeption("no money");
		}
	}
	public void withdrawHundred(int kilk) throws SQLException, NoMoneyExeption{
		int[] money =getMoneyUAN(); 
		if (money[2]>kilk){
			putMoneyUAN(money[0],money[1],money[2]-kilk,money[3]);
		}
		else{
			throw new  NoMoneyExeption("no money");
		}
	}
	public void withdrawTwoHandred(int kilk) throws SQLException, NoMoneyExeption{
		int[] money =getMoneyUAN(); 
		if (money[3]>kilk){
			putMoneyUAN(money[0],money[1],money[2],money[3]-kilk);
		}
		else{
			throw new  NoMoneyExeption("no money");
		}
	}
    int[] getMoneyUAN() throws SQLException{
        

   	          stmt = con.createStatement();
   	       rs = stmt.executeQuery("SELECT NUMDER_10 , NUMDER_20 ,NUMDER_100 , NUMDER_200 FROM ATM  WHERE CURRENCY = 'UAN' ");
   	       int[] mas= new int[4];
   	       if(rs.next()){
   	    	   mas[0]=rs.getInt(1);
   	    	   mas[1]=rs.getInt(2);
   	    	   mas[2]=rs.getInt(3);
   	    	   mas[3]=rs.getInt(4);
   	       }
   	       return mas;
   }
    
   /* void putMoneyEUR(int amount1,int amount2,int amount3,int amount4) throws SQLException{
        

   	 PreparedStatement ps = con.prepareStatement(
   		      "UPDATE ATM SET NUMDER_10 = ? , NUMDER_20= ? ,NUMDER_100= ? , NUMDER_200= ?  WHERE CURRENCY = ? ");

   		   
   		    ps.setInt(1,amount1);
   		    ps.setInt(2,amount2);
   		    ps.setInt(3,amount3);
   		    ps.setInt(4,amount4);
   		    ps.setString(5,"EUR");
   		    ps.executeUpdate();
   		    ps.close();
   }*/
  /*  void putMoneyUSD(int amount1,int amount2,int amount3,int amount4) throws SQLException{
        

   	 PreparedStatement ps = con.prepareStatement(
   		      "UPDATE ATM SET NUMDER_10 = ? , NUMDER_20= ? ,NUMDER_100= ? , NUMDER_200= ?  WHERE CURRENCY = ? ");

   		   
   		    ps.setInt(1,amount1);
   		    ps.setInt(2,amount2);
   		    ps.setInt(3,amount3);
   		    ps.setInt(4,amount4);
   		    ps.setString(5,"USD");
   		    ps.executeUpdate();
   		    ps.close();
   }*/
 /*  public static void main(String[] args) throws SQLException{
	   MaintananceModel model = new MaintananceModel();
	   model.putMoneyUAN(100,100,100,100);
	 
	   ResultSet resSet=model.getAllInfoAboutBank();
	   try {
		while(resSet.next()) {
		       System.out.print(resSet.getString(1) + "\t");
		       System.out.print(resSet.getInt(2)+ "\t");
		       System.out.print(resSet.getInt(3)+ "\t");
		       System.out.print(resSet.getInt(4)+ "\t");
		       System.out.println(resSet.getInt(5)+ "\t");
		   }
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	   //model.putMoneyUSD(100,100,100,100);
	 //  model.putMoneyEUR(100,100,100,100);
	
	   resSet=model.getAllInfoAboutBank();
	   try {
		while(resSet.next()) {
		       System.out.print(resSet.getString(1) + "\t");
		       System.out.print(resSet.getInt(2)+ "\t");
		       System.out.print(resSet.getInt(3)+ "\t");
		       System.out.print(resSet.getInt(4)+ "\t");
		       System.out.println(resSet.getInt(5)+ "\t");
		   }
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	  
   }
   */
   void toDoFinaly(){
	   try {
           rs.close();
           stmt.close();
           con.close();
       } catch (SQLException e) {
           e.printStackTrace();
       }
   }

}
